package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.domain.VideoChannels;
import com.ruoyi.business.domain.vo.BusCollectionItemVO;
import com.ruoyi.business.service.IBusAcquisitionCardService;
import com.ruoyi.business.service.IVideoChannelsService;
import com.ruoyi.common.constant.ScheduleConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.service.ISysJobService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.BusCollectionItemMapper;
import com.ruoyi.business.domain.BusCollectionItem;
import com.ruoyi.business.service.IBusCollectionItemService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 采集项Service业务层处理
 * 
 * @author asher
 * @date 2021-11-03
 */
@Service
public class BusCollectionItemServiceImpl implements IBusCollectionItemService 
{
    @Autowired
    private BusCollectionItemMapper busCollectionItemMapper;

    @Autowired
    private ISysJobService sysJobService;

    @Autowired
    private IVideoChannelsService videoChannelsService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private IBusAcquisitionCardService busAcquisitionCardService;

    /**
     * 查询采集项
     * 
     * @param collectionItemId 采集项主键
     * @return 采集项
     */
    @Override
    public BusCollectionItem selectBusCollectionItemByCollectionItemId(Long collectionItemId)
    {
        return busCollectionItemMapper.selectBusCollectionItemByCollectionItemId(collectionItemId);
    }

    /**
     * 查询采集项列表
     * 
     * @param busCollectionItem 采集项
     * @return 采集项
     */
    @Override
    public List<BusCollectionItem> selectBusCollectionItemList(BusCollectionItem busCollectionItem)
    {
        return busCollectionItemMapper.selectBusCollectionItemList(busCollectionItem);
    }

    /**
     * 新增采集项
     * 
     * @param busCollectionItem 采集项
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBusCollectionItem(BusCollectionItem busCollectionItem) {

        // 视频采集任务
//        if (busCollectionItem.getType().equals("1")) {
//            VideoChannels videoChannels = videoChannelsService.selectVideoChannelsById(busCollectionItem.getChannelId());
//            SysJob startSysJob = buildSysJob();
//            startSysJob.setJobName(busCollectionItem.getName() + " - 通道" + busCollectionItem.getChannelId() + " - 开始自动采集任务");
//            startSysJob.setCronExpression(DateUtils.parseDateToStr("ss mm HH dd MM ? yyyy", busCollectionItem.getStartTime()));
//            startSysJob.setInvokeTarget("asherVideoRecordTask.startRecordTask('" + videoChannels.getMediaserverid() + "', '" + videoChannels.getMainid() + "')");
//            SysJob endSysJob = buildSysJob();
//            endSysJob.setJobName(busCollectionItem.getName() + " - 通道" + busCollectionItem.getChannelId() + " - 结束自动采集任务");
//            endSysJob.setCronExpression(DateUtils.parseDateToStr("ss mm HH dd MM ? yyyy", busCollectionItem.getEndTime()));
//            endSysJob.setInvokeTarget("asherVideoRecordTask.stopRecordTask('" + videoChannels.getMediaserverid() + "', '" + videoChannels.getMainid() + "')");
//
//            try {
//                sysJobService.insertJobAutoRun(startSysJob);
//                sysJobService.insertJobAutoRun(endSysJob);
//                redisCache.setCacheObject(videoChannels.getMediaserverid() + "-" + videoChannels.getMainid() + "START_JOB", startSysJob.getJobId());
//                redisCache.setCacheObject(videoChannels.getMediaserverid() + "-" + videoChannels.getMainid() + "END_JOB", endSysJob.getJobId());
//            } catch (Exception e) {
//                redisCache.deleteObject(videoChannels.getMediaserverid() + "-" + videoChannels.getMainid() + "START_JOB");
//                redisCache.deleteObject(videoChannels.getMediaserverid() + "-" + videoChannels.getMainid() + "END_JOB");
//                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//                System.out.println(e);
//            }
//
//        }
        // 物理值采集配置
        if (busCollectionItem.getType().equals("0")) {
            busCollectionItem.setDeviceCode(busAcquisitionCardService.selectBusAcquisitionCardByAcquisitionCardId(busCollectionItem.getAcquisitionCardId()).getDeviceCode());
        }
        busCollectionItem.setCreateTime(DateUtils.getNowDate());
        return busCollectionItemMapper.insertBusCollectionItem(busCollectionItem);
    }

    /**
     * 修改采集项
     * 
     * @param busCollectionItem 采集项
     * @return 结果
     */
    @Override
    public int updateBusCollectionItem(BusCollectionItem busCollectionItem)
    {
        busCollectionItem.setUpdateTime(DateUtils.getNowDate());
        busCollectionItem.setDeviceCode(busAcquisitionCardService.selectBusAcquisitionCardByAcquisitionCardId(busCollectionItem.getAcquisitionCardId()).getDeviceCode());
        return busCollectionItemMapper.updateBusCollectionItem(busCollectionItem);
    }

    /**
     * 批量删除采集项
     * 
     * @param collectionItemIds 需要删除的采集项主键
     * @return 结果
     */
    @Override
    public int deleteBusCollectionItemByCollectionItemIds(Long[] collectionItemIds)
    {
        return busCollectionItemMapper.deleteBusCollectionItemByCollectionItemIds(collectionItemIds);
    }

    /**
     * 删除采集项信息
     * 
     * @param collectionItemId 采集项主键
     * @return 结果
     */
    @Override
    public int deleteBusCollectionItemByCollectionItemId(Long collectionItemId)
    {
        return busCollectionItemMapper.deleteBusCollectionItemByCollectionItemId(collectionItemId);
    }

    @Override
    public List<BusCollectionItemVO> selectBusCollectionItemDeepList(BusCollectionItem busCollectionItem) {
        return busCollectionItemMapper.selectBusCollectionItemDeepList(busCollectionItem);
    }

    private SysJob buildSysJob() {
        SysJob startSysJob = new SysJob();
        startSysJob.setJobGroup("DEFAULT");
        startSysJob.setMisfirePolicy(ScheduleConstants.MISFIRE_FIRE_AND_PROCEED);
        startSysJob.setConcurrent("0");
        return startSysJob;
    }
}
